<?php
/**
 * This is where PayPal customers land after purchasing.
 * 
 * if they are logged in, look for their memberships
 * verifysign=
 * 
 * we're passing the session id through and expect this to match, when it does, we add the membership
 * 
 */

include('HTGPage.class.php');

$page = new HTGPage();
/* @var $utils Utils */
$utils = $page->myUtils;
$authKey = $_COOKIE['PHPSESSID'];

if(!($page->isLoggedIn())) {
	$page->content .= 'Please go Log in.  If you have not created an account <a href="getit.php?from=ppl">click here</a> to <a href="getit.php?from=ppl">register</a>, and that will log you in.</a>';
	$page->display();
	die();
}

//get the most recent PurchaseAttempt with pa_status = 'p'
$purchaseAttempts = $utils->getPurchaseAttempts($authKey, 'p', 'Completed');


if(count($purchaseAttempts) > 0 ) {
	//get the first one -- should be most recent
	$purchaseAttempt = $purchaseAttempts[0];

	//create purchases	
	$purchaseId = $utils->addPurchase($purchaseAttempt['pa_id'], $purchaseAttempt['pa_ppp_id'], $_SESSION['u_id']);
	
	//create Membership
	$membershipId = $utils->addMembership($_SESSION['u_id'], $purchaseAttempt['pa_ppp_id']);

	//create MembershipPurchase
	$mpId = $utils->addMembershipPurchase($membershipId, $purchaseId); 

	$page->content .= 'Thanks for your purchase.  We just sent an email to' . $_SESSION['u_email'] . '.  <a href="nfl.php">Click here</a> to head over to our NFL picks page and thanks for feeding our familes.';
	$page->display();
	die();	
} else {
	$page->content .= '<span class="error">We weren\'t able to find your purchase. Please send an email to halftimegenius@gmail.com</span><br />';
}